clear;
clc;
close all;
addpath('../02_functions');

%------------------------------------------------------------------------%
% This file creates subfigures for figure 4 in the paper 
%------------------------------------------------------------------------%


% ---------------------------------------------------------------------------------- %

FS = 18;
FS2 = 32;
BinNum = 100;

% ---------------------------------------------------------------------------------- %

% choose sample period
start_date              = datenum(1998,1,5);
finish_date             = datenum(2020,12,27);

% ---------------------------------------------------------------------------------- %

% QUOTES
file_name           = '../01_data/es_quotes_5minute_EST';

% Compute returns

winsorizing         = 0;
alpha               = 0.99;
replace_option      = 1;

results             = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
Y                   = results.log_return_HbH_AD;
[T,H]               = size(Y);


% ---------------------------------------------------------------------------------- %

% number of bootstrap samples
BN     = 10000; 

% length of one block
alpha   = 0.05;       % for confidence intervals
BStar   = opt_block_length_REV_dec07(Y);
BS      = round(BStar(2));
%BS = floor(T/3);

% format lhv
[~, bb_indices]=block_bootstrap(Y(:,1),BN,BS);

% % New Y because of the way the way the sampling works.
Y = [Y ; Y(1:BS-1,:)];

% build cube to bootstrap
bb_Y = NaN(T,H,BN);

for i = 1 : BN
    bb_Y(:,:,i) = Y(bb_indices(:,i),:);
end

% compute bootstrapped statistics.
muX                     = NaN(BN,H);
tstatsX                 = NaN(BN,H);
pvaluesX                = NaN(BN,H);

for b = 1 : BN
    temp  = 0;
    for h = 1 : H
        LHV             = bb_Y(:,h,b);
        RHV             = ones(T,1);
        results_olsgmm  = olsgmm(LHV,RHV,1,1);
        muX(b,h)        = 100*100*results_olsgmm.beta;
        tstatsX(b,h)    = results_olsgmm.tstat;
        tcdf_lower      = tcdf(tstatsX(b,h),T-1);
        tcdf_upper      = tcdf(tstatsX(b,h),T-1,'upper');
        temp(1,h)       = 2*min(tcdf_lower,tcdf_upper);
    end
     pvaluesX(b,:)     = fwer_bonf(temp,alpha)';
    
end


% ---------------------------------------------------------------------------------- %


% calculations

% means

mu_vec          = mat2vec(muX);
mu_7            = muX(:,7);
mu_8            = muX(:,8);
mu_9            = muX(:,9);
mu_16           = muX(:,16);
mu_24           = muX(:,24);

mu_7est         = quantile(mu_7,0.50);
mu_8est         = quantile(mu_8,0.50);
mu_9est         = quantile(mu_9,0.50);
mu_16est        = quantile(mu_16,0.50);
mu_24est        = quantile(mu_24,0.50);

% t-statistics

t_vec           = mat2vec(tstatsX);
t_7             = tstatsX(:,7);
t_8             = tstatsX(:,8);
t_9             = tstatsX(:,9);
t_16            = tstatsX(:,16);
t_24            = tstatsX(:,24);

t_7est         = quantile(t_7,0.50);
t_8est         = quantile(t_8,0.50);
t_9est         = quantile(t_9,0.50);
t_16est        = quantile(t_16,0.50);
t_24est        = quantile(t_24,0.50);


% ---------------------------------------------------------------------------------- %



CI_vec          = quantile(mu_vec , [0+alpha 1-alpha])';
CI_OD           = quantile(mu_9   , [0+alpha 1-alpha])';


figure
h = histogram(mu_vec,BinNum,'Normalization','pdf');
h.FaceColor = rgb('grey');
hold on
plot([CI_vec(1) CI_vec(1)], [0 1],'--b','Linewidth',4,'Color','blue');
hold on
plot([CI_vec(2) CI_vec(2)], [0 1],'--b','Linewidth',4,'Color','blue');
hold on
plot([mu_7est mu_7est], [0 0.70],'--b','Linewidth',4,'Color','black');
hold on
h1 = text(mu_7est-0.1,0.70,'$24-1$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90);
hold on
text(mu_8est+0.1,0.70,'$1-2$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
hold on
plot([mu_9est mu_9est], [0 0.70],'--b','Linewidth',4,'Color','black');
hold on
text(mu_9est,0.70,'$2-3$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
plot([mu_16est mu_16est], [0 0.70],'--b','Linewidth',4,'Color','black');
hold on
text(mu_16est,0.70,'$9-10$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
hold on
plot([mu_24est mu_24est], [0 0.70],'--b','Linewidth',4,'Color','black');
hold on
text(mu_24est,0.70,'$17-18$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
xlim([-2.5 , 2.5])
legend off


figure;
h = histogram(mu_9,BinNum,'Normalization','pdf');
h.FaceColor = rgb('grey');
hold on
plot([CI_OD(1) CI_OD(1)], [0 2],'--b','Linewidth',4,'Color','blue');
hold on
plot([CI_OD(2) CI_OD(2)], [0 2],'--b','Linewidth',4,'Color','blue');
hold on
xlim([0.50 , 2.5])
legend off



% ---------------------------------------------------------------------------------- %

CI_vec          = quantile(t_vec , [0+alpha 1-alpha])';
CI_OD           = quantile(t_9  , [0+alpha 1-alpha])';


figure
h = histogram(t_vec,BinNum,'Normalization','pdf');
h.FaceColor = rgb('grey');
hold on
plot([CI_vec(1) CI_vec(1)], [0 0.5],'--b','Linewidth',4,'Color','blue');
hold on
plot([CI_vec(2) CI_vec(2)], [0 0.5],'--b','Linewidth',4,'Color','blue');
hold on
plot([t_7est t_7est], [0 0.15],'--b','Linewidth',4,'Color','black');
hold on
h1 = text(t_7est-0.15,0.15,'$24-1$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90);
hold on
text(t_8est+0.25,0.15,'$1-2$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
hold on
plot([t_9est t_9est], [0 0.15],'--b','Linewidth',4,'Color','black');
hold on
text(t_9est,0.15,'$2-3$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
hold on
plot([t_16est t_16est], [0 0.15],'--b','Linewidth',4,'Color','black');
hold on
text(t_16est,0.15,'$9-10$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
hold on
plot([t_24est t_24est], [0 0.15],'--b','Linewidth',4,'Color','black');
hold on
text(t_24est,0.15,'$17-18$','Color','black','FontSize',FS2,'Color','black','Interpreter','Latex','Rotation',90)
xlim([-7 , 11])
legend off



figure;
h = histogram(t_9,BinNum,'Normalization','probability');
h.FaceColor = rgb('grey');
hold on
plot([CI_OD(1) CI_OD(1)], [0 0.12],'--b','Linewidth',4,'Color','blue');
hold on
plot([CI_OD(2) CI_OD(2)], [0 0.12],'--b','Linewidth',4,'Color','blue');
hold on
legend off




